package 类与对象.多态.多态数组;

public class Main {
    public static void main(String[] args) {
        Person[] persons = new Person[5];

        persons[0] = new Person("小西", 20);
        persons[1] = new Student("小南", 16, 1200);
        persons[2] = new Student("小贝", 16,1500);
        persons[3] = new Teacher("tom", 20,15000);
        persons[4] = new Teacher("jack", 20,20000);

        //person 编译类型是Person, 运行类型是根据实际情况JVM来判断
        for (Person person : persons) {
//            System.out.println(person.say());
            if(person instanceof Student){
                Student student = (Student) person;//向下转型
                student.student();
                System.out.println(student.say());
            }else if(person instanceof Teacher){
                Teacher teacher = (Teacher) person;//向下转型
                teacher.teacher();
                System.out.println(teacher.say());
            }else if(person instanceof Person){
                System.out.println();
            } else {
                System.out.println("类型有误");
            }

        }

    }
}
